﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Diagnostics;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace TweakSP.Deployment
{
    class TweakSPInstallationReceiver : Microsoft.SharePoint.SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            Debug.WriteLine("TweakSPInstallationReceiver:FeatureActivated");

            SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;

            DeploymentUtilities.DeleteTimerJob(webApp, TweakSPInstallationJob.JOB_NAME);

            // Do the install Job
            TweakSPInstallationJob installJob = new TweakSPInstallationJob(TweakSPInstallationJob.JOB_NAME, webApp, properties.Definition.DisplayName);
            installJob.Title = TweakSPInstallationJob.JOB_TITLE;
            installJob.Schedule = new SPOneTimeSchedule(DateTime.Now);
            installJob.Update();


            if (webApp.IsAdministrationWebApplication)
            {
                Debug.WriteLine("Activating TweakSPInstallationReceiver on Central Administration Web Application");
                //    Activate the TweakSP_Admin Feature
                Guid adminFeatureId = new Guid("3310BF47-D252-454a-9424-C725E41129A3");
                string centralAdminUrl = DeploymentUtilities.GetCentralAdminWebUrl();

                DeploymentUtilities.AddFeatureToWeb(centralAdminUrl, adminFeatureId);

                DeploymentUtilities.InstallContentService();
            }
            else
            {
                Debug.WriteLine("Activating TweakSPInstallationReceiver on standard Web Application");
                //    Activate the TweakSP_Web WebApplication Feature
                Guid webFeatureId = new Guid("70D81404-03A8-4d7b-85B3-C69380D8B65B");
                string webUrl = webApp.AlternateUrls[0].IncomingUrl;

                DeploymentUtilities.AddFeatureToWebApplication(webUrl, webFeatureId);
            }

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            Debug.WriteLine("TweakSPInstallationReceiver:FeatureDeactivating");

            SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;

            if (webApp.IsAdministrationWebApplication)
            {
                Debug.WriteLine("Deactivating TweakSPInstallationReceiver on Central Administration Web Application");
                //    Activate the TweakSP_Admin Feature
                Guid adminFeatureId = new Guid("3310BF47-D252-454a-9424-C725E41129A3");
                string centralAdminUrl = DeploymentUtilities.GetCentralAdminWebUrl();

                DeploymentUtilities.RemoveFeatureFromWeb(centralAdminUrl, adminFeatureId);
            }
            else
            {
                Debug.WriteLine("Deactivating TweakSPInstallationReceiver on standard Web Application");
                //    Activate the TweakSP_Web WebApplication Feature
                Guid webFeatureId = new Guid("70D81404-03A8-4d7b-85B3-C69380D8B65B");
                string webUrl = webApp.AlternateUrls[0].IncomingUrl;

                DeploymentUtilities.RemoveFeatureToWebApplication(webUrl, webFeatureId);
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            Debug.WriteLine("TweakSPInstallationReceiver:FeatureUninstalling");
        }
    }
}
